package com.example.demo.control;

import com.example.demo.pojo.MultiChoice;
import com.example.demo.pojo.SingleChoice;
import com.example.demo.pojo.TFChoice;
import com.example.demo.common.CommonResult;
import com.example.demo.service.MultiChoiceService;
import com.example.demo.service.SingleChoiceService;
import com.example.demo.service.TFChoiceService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "习题接口")
@RestController
public class SelectController {

    @Autowired
    private SingleChoiceService singleChoiceService;
    @Autowired
    private MultiChoiceService multiChoiceService;
    @Autowired
    private TFChoiceService tfChoiceService;


    @ApiOperation(value = "获得题库")
    @ApiResponses({
            @ApiResponse(code = 200, message = "正常返回，返回格式为\n" +
                    "单选题{...}\n" +
                    "多选题{...}\n" +
                    "判断题{...}\n"),

    })
    @GetMapping("/queryPractice")
    public CommonResult queryPractice(){

        try {
            JSONArray jsonReturn = new JSONArray();
            JSONObject json;

            List<SingleChoice>singleChoiceList=singleChoiceService.getAll();
            json=new JSONObject();
            json.put("type","单选题");
            json.put("count",singleChoiceList.size());
            json.put("list",singleChoiceList);
            jsonReturn.add(json);

            List<MultiChoice>multiChoiceList=multiChoiceService.getAll();
            json=new JSONObject();
            json.put("type","多选题");
            json.put("count",multiChoiceList.size());
            json.put("list",multiChoiceList);
            jsonReturn.add(json);

            List<TFChoice>tfChoiceList=tfChoiceService.getAll();
            json=new JSONObject();
            json.put("type","判断题");
            json.put("count",tfChoiceList.size());
            json.put("list",tfChoiceList);
            jsonReturn.add(json);

            return CommonResult.success(jsonReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "获得一道题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name="type",value="题目类型：1为单选题,2为多选题,3为判断题",required=true,paramType="Integer"),
            @ApiImplicitParam(name="id",value="该类型题目对应的id字段",required=true,paramType="Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "正常返回,返回格式为\n" +
                    "title：题目\n" +
                    "result：答案\n" +
                    "option：选项\n"),
    })
    @PostMapping("/getDetail")
    public CommonResult getDetail(@RequestParam int type,@RequestParam int id){

        switch(type){
            case 1:
                return CommonResult.success(singleChoiceService.getById(id));
            case 2:
                return CommonResult.success(multiChoiceService.getById(id));
            case 3:
                return CommonResult.success(tfChoiceService.getById(id));
            default:
                break;
        }

        return CommonResult.failed();

    }
}
